/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.veditor.model;

import java.util.HashMap;
import java.util.HashSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import offset.nodes.Constants;

/**
 *
 * @author Walter Lütgenau
 */
public class SecondaryQueryAdjuster {

    String html;
    HashMap<String, String> secondaryQueries;
    static Pattern pattern = Pattern.compile(DataModel.SECONDARY_QUERY + "[0-9]*");

    public SecondaryQueryAdjuster(String html, HashMap<String, String> secondaryQueries) {
        this.html = html;
        this.secondaryQueries = secondaryQueries;
    }

    public void adjust() {
        if (html.indexOf(DataModel.SECONDARY_QUERY) < 0)
            return;

        Matcher matcher = pattern.matcher(html);

        StringBuilder buf = new StringBuilder();

        int start = 0;
        HashMap<String, String> substitutedQueries = new HashMap<String, String>();
        while (matcher.find()) {
            buf.append(html.substring(start, matcher.start()));

            String query = matcher.group().substring(1);
            String type = getType(html.substring(matcher.end()));

            if (isTopLevel(type))
                if (secondaryQueries.containsKey(query)) {
                    String substitutedQuery = DataModel.SECONDARY_QUERY.substring(1) + (secondaryQueries.size() + 1);
                    substitutedQueries.put(query, substitutedQuery);

                    secondaryQueries.put(substitutedQuery, type);

                    query = substitutedQuery;
                } else
                    secondaryQueries.put(query, type);
            else if (substitutedQueries.containsKey(query))
                query = substitutedQueries.get(query);

            buf.append(Constants.PATH_SEPARATOR + query);
            start = matcher.end();
        }

        buf.append(html.substring(start));
        html = buf.toString();
    }

    protected boolean isTopLevel(String query) {
        return query.indexOf(Constants.PATH_SEPARATOR) < 0;
    }

    protected String getType(String secondaryQuery) {
        int start = secondaryQuery.indexOf(Constants.PATH_SEPARATOR) + 1;
        int end = secondaryQuery.indexOf("\"");

        return secondaryQuery.substring(start, end);
    }

    public HashMap<String, String> getSecondaryQueries() {
        return secondaryQueries;
    }

    public String getHtml() {
        return html;
    }
}
